package test;

/**
 * 两个使用字符串表示的大整数相加求和，不能使用BigDecimal类。
 * <p>
 * 比如：
 * String str1 = "91253479592133208796503";
 * String str2 = "342390903333232998";
 * public String add(String str1, String str2){
 * }
 * <p>
 * 获取长度，转char[]倒序，相加，在倒序
 */
public class Test20210413question1 {
    public static void main(String[] args) {
        String str1 = "91253479592133208796503";
        String str2 =      "342390903333232998";
        String result = solution0413(str1, str2);
        System.out.println("相加结果为" + result);
    }

    public static String solution0413(String str1, String str2) {
        int length1 = str1.length();
        int length2 = str2.length();
        int finalLength = length1 >= length2 ? length1 : length2;
        char[] str11 = str1.toCharArray();
        char[] str21 = str2.toCharArray();
        char[] str11rever = reverseExample(str11, length1);
        char[] str21rever = reverseExample(str21, length2);
        char[] final1 = transfer(str11rever,length1,str21rever,length2);
        return String.valueOf(reverseExample(final1,finalLength));
    }

    /**
     * char数组倒序
     * @param a
     * @param length
     * @return
     */
    public static char[] reverseExample(char[] a, int length) {
        char[] rever = new char[length];
        for (int i = 0; i < length; i++) {
            rever[length - i - 1] = a[i];
        }
        return rever;
    }

    /**
     * 将长度小的数组补0
     * @param a
     * @param alength
     * @param b
     * @param blength
     * @return
     */
    public static char[] transfer(char[] a, int alength, char[] b, int blength) {
        int finalLength = alength >= blength ? alength : blength;
        char[] c = new char[finalLength];
        char[] d = new char[finalLength];

        if (alength > blength) {
            for (int i1 = 0; i1 < blength; i1++) {
                c[i1] = b[i1];
            }
            for (int i11 = blength; i11 < alength; i11++) {
                c[i11] = '0';
            }
            d = add(a, c,finalLength);
        } else {
            for (int i2 = 0; i2 < alength; i2++) {
                c[i2] = a[i2];
            }
            for (int i11 = alength; i11 < blength; i11++) {
                c[i11] = '0';
            }
            d = add(b, c,finalLength);
        }
        return d;
    }

    /**
     * 相加
     * @param a
     * @param b
     * @param length
     * @return
     */
    public static char[] add(char[] a, char[] b,int length) {

        char[] chars = new char[length];
        //进位
        int flag = 0;
        int num = 0;
        for (int i = 0; i < length; i++) {
            num = ((a[i]-'0') + (b[i]-'0')+flag);
            if (num / 10 == 0) {
                flag = 0;
            } else {
                flag = 1;
                num = num % 10;
            }
            chars[i]= (char)(num+'0');
        }

        return chars;
    }
}
